package clf.wajl.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.UUID;

import clf.wajl.Global;
import clf.wajl.R;
import clf.wajl.ui.AnswerPopMenu;
import clf.wajl.ui.PopMenu;
import clf.wajl.utils.ScreenTools;
import zhy.imageloader.PhotoSelectActivity;

/**
 * Created by s on 2017/9/20.
 */

public class AskActivity extends BaseActivity implements View.OnTouchListener {

	private ImageView askPicIV;
	private String picPath;
	private AlertDialog addLocationDialog;
	private int xRiddle;
	private int yRiddle;
	private String askId;
	private LinkedList<String> riddleList = new LinkedList<>();
	private LinkedList<String> locationList=new LinkedList<>();
	private AlertDialog addRiddleDialog;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ask);

		initData();
		initWidget();

	}

	private void initData() {
		SharedPreferences sp = getSharedPreferences(Global.CONFIG_NAME, 0);

		askId = UUID.randomUUID().toString();
		int currentW = ScreenTools.instance(this).getScreenWidth();
		int currentH = ScreenTools.instance(this).getScreenHeight();
		String askScreenWH = sp.getString(Global.ASK_SRCEEN_W_H + askId, String.format("%s&%s", currentW, currentH));

		showPicDialog();

	}

	private void showPicDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示");
		builder.setMessage("请导入一张图片");
		builder.setPositiveButton("导入", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int i) {

				Intent intent = new Intent(AskActivity.this, PhotoSelectActivity.class);
				intent.putExtra("picMaxSize", 1);
				startActivityForResult(intent, 100);
				dialogInterface.dismiss();

			}
		});
		builder.setNeutralButton("退出", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int i) {
				onBackPressed();
			}
		});

		builder.create().show();
	}


	private void initAddLocationDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示");
		builder.setMessage("请选择谜语对应的位置。");
		builder.setPositiveButton("好的", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int i) {

				dialogInterface.dismiss();

			}
		});

		addLocationDialog = builder.create();
	}

	private void initAddRiddleDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示");

		View view = View.inflate(this, R.layout.dialog_add_riddle, null);
		final EditText riddleET = view.findViewById(R.id.et_add_riddle);


		builder.setView(view);
		builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int i) {

				String riddle = riddleET.getText().toString();
				if (TextUtils.isEmpty(riddle)) {
					Toast.makeText(AskActivity.this, "请输入谜语！", Toast.LENGTH_SHORT).show();
				} else {

					String wh = String.format("%s&&%s", xRiddle, yRiddle);
					locationList.addLast(wh);
					riddleList.addLast(riddle);
					dialogInterface.dismiss();

				}

			}
		});
		builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int i) {

				xRiddle = 0;
				yRiddle = 0;
				dialogInterface.dismiss();
			}
		});

		addRiddleDialog = builder.create();
	}

	private void initWidget() {
		askPicIV = findViewById(R.id.iv_ask_pic);
		askPicIV.setOnTouchListener(this);


		initAddLocationDialog();
		initAddRiddleDialog();
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
			case 100:

				if (data != null) {
					try {
						fillImageSelect(data);
						addLocationDialog.show();
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				break;
		}
	}

	private void fillImageSelect(Intent data) throws JSONException {

		JSONTokener jsonTokener = new JSONTokener(data.getStringExtra("photoUrl"));
		if (null != data.getStringExtra("photoUrl") && !"".equals(data.getStringExtra("photoUrl"))) {
			JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
			JSONArray photoUrls = jsonObject.getJSONArray("photoUrl");
			picPath = photoUrls.getString(0);
			File pic = new File(picPath);
			Glide.with(AskActivity.this).load(pic).into(askPicIV);

		}else{
			Toast.makeText(AskActivity.this,"没有图片无法进行下面的操作。",Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public boolean onTouch(View view, MotionEvent motionEvent) {


		switch (motionEvent.getAction()) {
			case MotionEvent.ACTION_UP:
				settingRiddle(motionEvent);
				break;
		}
		return false;
	}

	private void settingRiddle(MotionEvent motionEvent) {

		//获取坐标，设置谜语

		if (riddleList.size()>=3){
			Toast.makeText(AskActivity.this,"当前版本只支持三个谜语。",Toast.LENGTH_SHORT).show();
		}else{
			xRiddle = (int) motionEvent.getX();
			yRiddle = (int) motionEvent.getY();
			addRiddleDialog.show();
		}

	}
}
